﻿using EnglishAuction.BUS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EnglishAuction
{
    public partial class EditProduct : System.Web.UI.Page
    {
        private int id;

        private void FillData()
        {
            
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            txtGiaThiTruong.Attributes.Add("onkeypress", "return isNumberKey(event)");
            
            if (!IsPostBack)
            {
                txtNgayLenSan.Text = String.Format("{0:MM/dd/yyyy hh:mm tt}", DateTime.Now.AddDays(1));
                if (int.TryParse(Request.QueryString["Id"], out id))
                {

                }
                if (id > 0)
                {
                    SanPham sanPham = SanPhamBUS.GetById(id);
                    lbGiaSan.Text = String.Format("{0:###,###}", sanPham.GiaSan);
                    lbTenSanPham.Text = sanPham.TenSanPham;
                    lbNguoiDang.Text = NguoiDungBUS.GetById(sanPham.NguoiDungId.Value).TaiKhoan;
                    lbThoiGianDauGia.Text = sanPham.ThoiGianDauGia.ToString();
                    lbNgayDang.Text = String.Format("{0:hh:mm tt dd/MM/yyyy}", sanPham.NgayTao);
                    ddlLoaiSanPham.DataSource = LoaiSanPhamBUS.GetAll();
                    ddlLoaiSanPham.DataValueField = "Id";
                    ddlLoaiSanPham.DataTextField = "TenLoaiSanPham";
                    ddlLoaiSanPham.DataBind();
                    ddlLoaiSanPham.SelectedValue = sanPham.LoaiSanPhamId.ToString();
                    lbMoTaSanPham.Text = sanPham.MoTa;
                    imgHinhDaiDien.ImageUrl = "images/" + sanPham.HinhAnh;
                }
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Response.Redirect("ManageProduct.aspx"); 
        }

        protected void btnDeleteProduct_Click(object sender, EventArgs e)
        {
            if (txtLyDoXoa.Text.Equals(""))
            {
                string script = "$(function(){openDialog();setError('Phải nhập lý do xóa sản phẩm.')});";
                ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "dialog", script, true);
                return;
            }
            int t = int.Parse(Request.QueryString["Id"]);
            SanPham sanPham = SanPhamBUS.GetById(t);
            NguoiDung nguoiDung = NguoiDungBUS.GetById(sanPham.NguoiDungId.Value);
            Utilities.SendMail(nguoiDung.Email, txtLyDoXoa.Text);
            //File.Delete(Server.MapPath("~/images/" + sanPham.HinhAnh));
            if (SanPhamBUS.Delete(sanPham.Id))
            {
                Response.Redirect("~/ManageProduct.aspx");
            }
        }

        protected void btnDuyet_Click(object sender, EventArgs e)
        {
            if (txtGiaThiTruong.Text.Equals(""))
            {
                string script = "$(function(){openDialog();setError('Giá thị trường chưa được nhập.')});";
                ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "dialog", script, true);
                return;
            }
            int t = int.Parse(Request.QueryString["Id"]);

            SanPham sanPham = SanPhamBUS.GetById(t);

            DateTime ngayLenSan = DateTime.Parse(txtNgayLenSan.Text);
            double giaThiTruong = Utilities.Currency(txtGiaThiTruong.Text);
            sanPham.NgayLenSan = ngayLenSan;
            sanPham.GiaThiTruong = giaThiTruong;
            sanPham.TinhTrang = 2;
            if (SanPhamBUS.Update(sanPham))
            {
                Response.Redirect("~/ManageProduct.aspx");
            }
        }
    }
}